package com.yhd.admin.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yhd.admin.bean.User;
import com.yhd.admin.service.UserService;
import com.yhd.admin.utils.ServiceMessage;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping()
	public String showPage(){
		return "user";
	}
	
	@RequestMapping("/list")
	public @ResponseBody Object list(User user,int page,int rows){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", userService.getCount(user));
		List<User> Users = userService.getUsers(user, page, rows);
		map.put("rows", Users);
		return map;
	}
	
	@RequestMapping(value="/check/loginname",method=RequestMethod.GET)
	public @ResponseBody ServiceMessage add(String loginName){
		User user = new User();
		user.setLogin_name(loginName);
		user = userService.getUserByUser(user);
		return ServiceMessage.ok(user);
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String addPage(){
		return "user-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Object add(User user){
		userService.add(user);
		return ServiceMessage.ok();
	}
}
